import {
	mapMutations
} from 'vuex';
import Vue from 'vue';
import tools from 'utils/tools.js';
import base from 'utils/base.js';
export default {
	Auth,

}

function Auth(currentPage, option) {
	//清理名片二维码缓存
	uni.removeStorage({
		key: 'ScanCodeDistributionQrcode',
		success: function(res) {

		}
	});
	let userID = option.query.userID;
	let returnUrl = option.query.returnUrl;
	let openID = option.query.openId;
	let globalData = currentPage.globalData;
	if (globalData.debugUserID > 0) userID = globalData.debugUserID;

	var domain = tools.GetDomain();
	let paramsdmain = {
		type: "GET",
		url: "/api/Customer/GetCustomerByDomain",
		data: {
			domain: domain
		},
		sCallback: function(response) {
			if (response.IsSuccess) {
				let customer = response.Data;
				globalData.ZpAppId = customer.ZpAppId;
				globalData.Customer = customer;
				uni.setStorageSync('ZpAppId', customer.ZpAppId);
				uni.setStorageSync('Customer', customer);
				if (userID > 0) {
					var domain = tools.GetDomain();
					let params = {
						type: "GET",
						url: "/api/Authorization/LoginByUserID",
						data: {
							userID: userID,
							domain: domain
						},
						sCallback: function(response) {
							if (response.IsSuccess) {
								let ticket = response.Data.Ticket;
								uni.setStorageSync('ticket', ticket);
								globalData.ticket = ticket;
								let params1 = {
									type: 'GET',
									url: '/api/UserInfo/GetUserInfo',
									data: {},
									sCallback: function(response) {
										if (response.IsSuccess) {
											let userInfo = response.Data;
											globalData.UserInfo = userInfo;
											currentPage.UserInfo = userInfo;
											uni.setStorageSync('UserInfo', userInfo);
										}
									}
								};
								base.request(params1);

							}

						}
					}

					base.request(params);
				} else if (openID) {
					var domain = tools.GetDomain();
					let params = {
						type: "GET",
						url: "/api/Authorization/LoginByOpenID",
						data: {
							openid: openID,
							domain: domain,
							scope: "snsapi_base"
						},
						sCallback: function(response) {
							if (response.IsSuccess) {
								let ticket = response.Data.Ticket;
								uni.setStorageSync('ticket', ticket);
								globalData.ticket = ticket;
								let params2 = {
									type: 'GET',
									url: '/api/UserInfo/GetUserInfo',
									data: {},
									sCallback: function(response) {
										if (response.IsSuccess) {
											let userInfo = response.Data;
											globalData.UserInfo = userInfo;
											currentPage.UserInfo = userInfo;
											uni.setStorageSync('UserInfo', userInfo);
										}
									}
								};
								base.request(params2);
							}

						}
					};
					base.request(params);
				} else {
					let domain = window.location.hostname.split('.')[0];
					if (domain == 'localhost') domain = 'zhengpinkeji';
					let link = window.location.href; //'http://'+domain+'.member.onlyid.cn/#' + option.path;
					var url = globalData.WXCenterApi + "/connect/oauth2/authorize?ZpAppId=" + globalData.ZpAppId + "&state=" +
						globalData.ZpAppId + "&ReturnUrl=" + escape(link)
					window.location.href = url;

				}

				if (returnUrl) uni.setStorageSync('returnUrl', ticket);
			}

		}
	};
	base.request(paramsdmain);



}
